package io.renren.modules.app.aspect;

import cn.hutool.core.date.DateTime;
import io.renren.modules.app.annotation.UserLog;
import io.renren.modules.app.entity.UserLogEntity;
import io.renren.modules.app.service.UserLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Date;

/**
 * @Description: java类作用描述
 * @Author: beike
 * @CreateDate: 2021/4/4 0004$ 23:27$
 */
@Aspect
@Component
public class UserLogAspect {

    @Autowired
    private UserLogService userLogService;
    @Pointcut("@annotation(io.renren.modules.app.annotation.UserLog)")
    public void logPointCut(){

    }
    @After("logPointCut()")
    public void after( JoinPoint point ){
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        UserLogEntity userLogEntity = new UserLogEntity();
        UserLog userLog = method.getAnnotation(UserLog.class);
        if (userLog!=null){
            userLogEntity.setLogName(userLog.value());
        }
        String className = point.getTarget().getClass().getName();
        String methodName = signature.getName();
        System.out.println("方法名为+"+className+"."+methodName);
        userLogEntity.setLogMethod(methodName);
        userLogEntity.setCreateTime(new DateTime());
        userLogService.save(userLogEntity);

    }
}
